package cn.daniellee.plugin.wc.command;

import cn.daniellee.plugin.wc.Welcome;
import cn.daniellee.plugin.wc.core.WelcomeCore;
import cn.daniellee.plugin.wc.model.PlayerData;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.LinkedList;
import java.util.Random;

public class WelcomeCommand implements CommandExecutor {

    @Override
    public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
        if (strings.length > 0) {
            if ("reload".equalsIgnoreCase(strings[0]) && commandSender.isOp()) {
                Welcome.getInstance().reloadConfig();
                Welcome.getInstance().loadConfig();
                commandSender.sendMessage((Welcome.getInstance().getPrefix() + Welcome.getInstance().getConfig().getString("message.reload-success", "&eConfiguration reload completed.")).replace("&", "§"));
            } else if (commandSender instanceof Player) {
                WelcomeCore.handleWelcome((Player) commandSender, strings[0], true);
            }
        } else sendHelp(commandSender);
        return true;
    }


    private void sendHelp(CommandSender commandSender) {
        commandSender.sendMessage(("&m&a----&m&6----------&3 " + Welcome.getInstance().getConfig().getString("prompt-prefix", "Welcome") + " &m&6----------&m&a----").replace("&", "§"));

        String targetText = Welcome.getInstance().getConfig().getString("help.welcome", "Welcome new player!").replace("&", "§");
        TextComponent targetHelp = new TextComponent("/wc <player>  " + targetText);
        targetHelp.setColor(ChatColor.GRAY);
        targetHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/wc "));
        targetHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(targetText).color(ChatColor.BLUE).create()));
        commandSender.spigot().sendMessage(targetHelp);

        String reloadText = Welcome.getInstance().getConfig().getString("help.reload", "Reload configuration.").replace("&", "§");
        TextComponent reloadHelp = new TextComponent("/wc reload  " + reloadText);
        reloadHelp.setColor(ChatColor.GRAY);
        reloadHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/wc reload"));
        reloadHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(reloadText).color(ChatColor.BLUE).create()));
        commandSender.spigot().sendMessage(reloadHelp);
    }
}
